package users;

import utils.Utils;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import java.sql.Date;
import java.util.Calendar;

public class UsersFun {
    //检查复选框是否选中
    public static  boolean ischeck(JTable table){
        int count=0;
        for (int j = 0; j < table.getRowCount(); j++) {
            //获取复选框状态（是否选中）
            Boolean isSelected = (Boolean) table.getValueAt(j, 0);
            if (isSelected) {
                count++;
            }
        }
        if(count==0){
            return false;
        }
        else {
            System.out.println(count);
            return true;
        }
    }

    public static void queryone(String id, DefaultTableModel stumodel)throws Exception{
        if(!ManagerFun.isAllDigits(id)){
            throw new Exception();
        }
        stumodel.setRowCount(0);
        int id_1 = Integer.parseInt(id);
        System.out.println("querystu成功！");
        //连接mysql
        Connection connection= Utils.getConnection();
        //定义sql语句
        String sql="select * from book where id like ? and num!=0";
        PreparedStatement stmt = connection.prepareStatement(sql);
        stmt.setString(1, "%" + id + "%");
        ResultSet rs = stmt.executeQuery();
        while(rs.next()){
            int id_2 = rs.getInt("id");
            String name = rs.getString("name");
            String type = rs.getString("type");
            String publish = rs.getString("publish");
            String author = rs.getString("author");
            Double price = rs.getDouble("price");
            int num = 1;
            stumodel.addRow(new Object[]{Boolean.FALSE,id_2,name,type,publish,author,price,num});
        }
        Utils.Close(stmt,connection);
    }

    //查询全部图书信息（
    public static void queryall(DefaultTableModel stumodel)throws Exception{
        //System.out.println("querystu成功！");
        //连接mysql
        stumodel.setRowCount(0);
        Connection connection= Utils.getConnection();
        //定义sql语句
        String sql="select * from book where num!=0";
        Statement stmt = connection.createStatement();
        ResultSet rs=stmt.executeQuery(sql);
        while(rs.next()){
            int id_2 = rs.getInt("id");
            String name = rs.getString("name");
            String type = rs.getString("type");
            String publish = rs.getString("publish");
            String author = rs.getString("author");
            Double price = rs.getDouble("price");
            int num = 1;
            stumodel.addRow(new Object[]{Boolean.FALSE,id_2,name,type,publish,author,price,num});
        }
        Utils.Close(stmt,connection);
    }

    //ButBook和ShoppingCart中的购买按钮
    public static void purchase(JTable table,String username)throws Exception{
        //检查复选框是否选中
        if(!ischeck(table)){
            return;
        }
        //连接mysql
        Connection connection= Utils.getConnection();
        //定义sql语句
        String sql="insert into orders(username,date) values(?,?)";
        PreparedStatement stmt= connection.prepareStatement(sql);
        stmt.setString(1,username);
        // 获取当前日期时间
        Calendar calendar = Calendar.getInstance();

        java.util.Date utilDate = calendar.getTime();

        // 将java.util.Date转换为java.sql.Date
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

        stmt.setDate(2,sqlDate);
        stmt.executeUpdate();

        sql="select order_id from orders where username=? ORDER BY order_id desc limit 1;";
        stmt= connection.prepareStatement(sql);
        stmt.setString(1,username);
        ResultSet rs = stmt.executeQuery();
        int orderid=0;
        while(rs.next()){
            orderid=rs.getInt("order_id");
        }
        sql="insert into orders_info(order_id,book_id,num,price) values(?,?,?,?)";
        stmt= connection.prepareStatement(sql);
        for (int i = 0; i < table.getRowCount(); i++) {
            //获取复选框状态（是否选中）
            Boolean isSelected = (Boolean) table.getValueAt(i, 0);
            if (isSelected) {
                stmt.setInt(1,orderid);
                stmt.setInt(2, (Integer) table.getValueAt(i,1));
                stmt.setInt(3, Integer.parseInt(table.getValueAt(i,7).toString()));
                stmt.setDouble(4, Integer.parseInt(table.getValueAt(i,7).toString())*(Double) table.getValueAt(i,6));
                //执行sql语句
                stmt.executeUpdate();
                ManagerFun.updatenum((Integer) table.getValueAt(i,1),Integer.parseInt(table.getValueAt(i,7).toString()));
            }
        }
        Utils.Close(stmt,connection);
    }


    //BuyBook中的添加到购物车
    public static void addcart(JTable table,String username) throws Exception{
        //检查复选框是否选中
        if(!ischeck(table)){
            return;
        }
        Vector<Integer> vector = new Vector<>();
        //连接mysql
        Connection connection= Utils.getConnection();
        String sql="SELECT DISTINCT book_id FROM book_cart_info where username=?";
        PreparedStatement stmt=connection.prepareStatement(sql);
        stmt.setString(1,username);
        ResultSet rs1=stmt.executeQuery();
        while (rs1.next()){
            vector.add(rs1.getInt("book_id"));
        }

        String sql2="update cart_info set num=num+? where username=? and book_id=?";
        PreparedStatement stmt2=connection.prepareStatement(sql2);
        stmt2.setString(2,username);

        String sql3="INSERT INTO cart_info(username,book_id,num) VALUES(?,?,?)";
        PreparedStatement stmt3=connection.prepareStatement(sql3);
        stmt3.setString(1,username);

        for (int i = 0; i < table.getRowCount(); i++) {
            //获取复选框状态（是否选中）
            Boolean isSelected = (Boolean) table.getValueAt(i, 0);
            if (isSelected) {
                if(vector.contains((Integer)table.getValueAt(i,1))){
                    System.out.println("in");
                    stmt2.setInt(1, Integer.parseInt(table.getValueAt(i,7).toString()));
                    stmt2.setInt(3, (Integer) table.getValueAt(i,1));
                    stmt2.executeUpdate();
                }
                else{
                    System.out.println("not");
                    stmt3.setInt(2, (Integer) table.getValueAt(i,1));
                    stmt3.setInt(3, (Integer) table.getValueAt(3,7));
                    stmt3.executeUpdate();
                }

            }
        }
        Utils.Close(stmt,connection);
        Utils.Close(stmt2,connection);
        Utils.Close(stmt3,connection);
    }

    //删除购物车
    public static void deletecart(JTable table,String username)throws Exception{
        if(!ischeck(table)){
            return;
        }
        //连接mysql
        Connection connection= Utils.getConnection();
        //定义sql语句
        String sql="delete from cart_info where username=? and book_id=?";
        PreparedStatement stmt= connection.prepareStatement(sql);

        for (int i = 0; i < table.getRowCount(); i++) {
            //获取复选框状态（是否选中）
            Boolean isSelected = (Boolean) table.getValueAt(i, 0);
            if (isSelected) {
                stmt.setInt(2, (Integer) table.getValueAt(i,1));
                stmt.setString(1,username);
                //执行sql语句
                stmt.executeUpdate();
            }
        }

        Utils.Close(stmt,connection);
    }
    //查询我的所有订单
    public static void allorder(String username,DefaultTableModel stumodel)throws Exception{
        stumodel.setRowCount(0);
        Connection connection= Utils.getConnection();
        //定义sql语句
        String sql="select * from orders where username=? ";
        PreparedStatement stmt= connection.prepareStatement(sql);
        stmt.setString(1,username);
        ResultSet rs = stmt.executeQuery();

        String sql2="select * from book_orders_orders_info where order_id=? ";
        PreparedStatement stmt2= connection.prepareStatement(sql2);

        while(rs.next()){
            int id_2 = rs.getInt("order_id");
            String date = String.valueOf(rs.getDate("date"));
            stmt2.setInt(1,id_2);
            ResultSet rs2 = stmt2.executeQuery();
            while (rs2.next()){
                String name=rs2.getString("name");
                String author=rs2.getString("author");
                String publish=rs2.getString("publish");
                String type=rs2.getString("type");
                int num=rs2.getInt("num");
                Double price=num*rs2.getDouble("price");
                stumodel.addRow(new Object[]{id_2,date,name,author,type,publish,num,price});
            }

        }

        Utils.Close(stmt,connection);
    }
    //查询购物车信息
    public static void allcart(String username,DefaultTableModel stumodel)throws Exception{
        stumodel.setRowCount(0);
        //连接mysql
        Connection connection= Utils.getConnection();
        //定义sql语句
        String sql="SELECT * from book_cart_info  WHERE username=?;";
        PreparedStatement stmt = connection.prepareStatement(sql);
        stmt.setString(1,username);
        ResultSet rs=stmt.executeQuery();
        while(rs.next()){
            int book_id=rs.getInt("book_id");
            String name = rs.getString("name");
            String type = rs.getString("type");
            String publish = rs.getString("publish");
            String author = rs.getString("author");
            Double price = rs.getDouble("price");
            String num = String.valueOf(rs.getInt("num"));
            stumodel.addRow(new Object[]{Boolean.FALSE,book_id,name,type,publish,author,price,num});
        }
        Utils.Close(stmt,connection);
    }

    public static void cartone(String book_id, String username, DefaultTableModel stumodel)throws Exception {
        stumodel.setRowCount(0);
        //连接mysql
        Connection connection= Utils.getConnection();
        //定义sql语句
        String sql="SELECT * from book_cart_info  WHERE username=? and book_id like ?";
        PreparedStatement stmt = connection.prepareStatement(sql);
        stmt.setString(1,username);
        stmt.setString(2, "%" + Integer.parseInt(book_id) + "%");
        ResultSet rs=stmt.executeQuery();
        while(rs.next()){
            int book_id2=rs.getInt("book_id");
            String name = rs.getString("name");
            String type = rs.getString("type");
            String publish = rs.getString("publish");
            String author = rs.getString("author");
            Double price = rs.getDouble("price");
            String num = String.valueOf(rs.getInt("num"));
            stumodel.addRow(new Object[]{Boolean.FALSE,book_id2,name,type,publish,author,price,num});
        }
        Utils.Close(stmt,connection);
    }


}
